<?php
class Macys_Oauth_Authenticate_Mobile extends Macys_Oauth_Authenticate_BaseAuth {

	private $_username;
	private $_password;

	//protected $_auth_endpoint = '/v2/oauth2/token';
	protected $_auth_endpoint = '/token';

	public function __construct($config) {

		if (isset($config['username'])) {
	 	$this->_username = $config['username'];
	 	unset($config['username']);
		} else {
	 	$this->_username = null;
		}

		if (isset($config['password'])) {
			$this->_password = $config['password'];
			unset($config['password']);
		} else {
			$this->_password = null;
		}
		// Calling Parent Constructor to continue initialization process
		parent::__construct($config);
	}

	/**
	 * Use Username and password to Authroize
	 * (non-PHPdoc)
	 * @see Zend_Auth_Adapter_Interface::authenticate()
	 */
	public function authenticate() {
		$msg = 'Username or Password not set';
		if (empty($this->_username)) {
			$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
			return new Zend_Auth_Result($code, false, $msg);
		}
		if (empty($this->_password)) {
			$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
			return new Zend_Auth_Result($code, false, $msg);
		}

		$args = array(
		Macys_Constants::GRANTTYPE => 'password',
            'username'          => $this->_username,
			'password'          => $this->_password,
		);
		$url = $this->_auth_uri . $this->_auth_endpoint;
		$response = $this->_apiCall($url, $args);
		// Sets the Zend_Auth params
		$result = $this->setAuthentication($response);

		return $result;
	}


	public function setAuth($username, $password) {
		$this->_username = $username;
		$this->_password = $password;
	}

		
	protected function setAuthType($type='mobile'){
		
	}
}